<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="合同编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入合同编码" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发起事由" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reason">
              <a-textarea v-model="model.reason" rows="4" placeholder="请输入发起事由" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同类型  " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="purchase_contract_type" placeholder="请选择合同类型  " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="providerCode">
              <j-popup
                v-model="model.providerCode"
                field="providerCode"
                org-fields="code,name,contacts_phone"
                dest-fields="providerCode,providerName,providerPhone"
                code="purchase_provider"
                :multi="false"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="providerName">
              <a-input v-model="model.providerName" placeholder="请输入供应商名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="providerPhone">
              <a-input v-model="model.providerPhone" placeholder="请输入供应商联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="我方单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ourName">
              <a-input v-model="model.ourName" placeholder="请输入我方单位名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="金额类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moneyType">
              <j-dict-select-tag type="list" v-model="model.moneyType" dictCode="purchase_money_type" placeholder="请选择金额类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoiceType">
              <j-dict-select-tag type="list" v-model="model.invoiceType" dictCode="purchase_invoice_type" placeholder="请选择发票类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxRate">
              <j-dict-select-tag type="list" v-model="model.taxRate" dictCode="purchase_tax_rate" placeholder="请选择税率" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同签订日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signTime">
              <j-date placeholder="请选择合同签订日期" v-model="model.signTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择合同开始时间" v-model="model.startTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择合同结束时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="签订人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signBy">
              <j-select-user-by-dep v-model="model.signBy" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <j-upload v-model="model.attachment" :number=10 :before-upload="beforeUpload"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import {contractCodeRule} from '@/api/api'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'PurchaseContractForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          code: ''
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          reason: [
            { required: true, message: '请输入发起事由'},
            { min: 5, max: 200, message: '长度在5到200个字符', tigger: 'blur'}
          ],
          providerCode: [
            { required: true, message: '请选择供应商'},
          ],
          ourName: [
            { required: true, message: '请输入我方单位名称'},
            { min: 5, max: 50, message: '长度在5到50个字符', tigger: 'blur'}
          ],
          signTime: [
            { required: true, message: '请选择合同签订日期'},
          ],
          startTime: [
            { required: true, message: '请选择合同开始时间'},
          ],
          endTime: [
            { required: true, message: '请选择合同结束时间'},
          ],
          signBy: [
            { required: true, message: '请选择签订人'},
          ],
        },
        url: {
          add: "/purchase/purchaseContract/add",
          edit: "/purchase/purchaseContract/edit",
          queryById: "/purchase/purchaseContract/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      //初始化合同编码
      init(){
        contractCodeRule({}).then((res)=>{
          if(res.success){
            this.model.code = res.result;
          }else{
            this.$message.warning(res.message);
          }
        });
        // this.$refs.form.title="合同新增";
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(!this.model.id){
          this.init();
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
      beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('文件过大');
      }
      const isMax = this.model.attachment && this.model.attachment.split(',').length >= 10
      if(isMax){
        this.$message.error('最多上传文件数10个');
      }
      return isLt2M && !isMax;
    },
    }
  }
</script>